<?php

namespace App\Http\Controllers\Supplier;

use App\Models\Areas;
use App\Models\CategoryUser;
use App\Models\Goods;
use App\Models\GoodsCustom;
use App\Models\GoodsCustomfield;
use App\Models\GoodsCustomRent;
use App\Models\GoodsLog;
use App\Models\GoodsSetmeal;
use App\Models\GoodsHuabei;
use App\Models\GoodsSpec;
use App\Models\MemberPromise;
use App\Models\ShopUser;
use App\Requests\Admin\Goods\GoodsSaveRequest;
use App\Services\AlipayService;
use Illuminate\Http\Request;
use App\Services\CommonService;
use App\Services\GoodsService;
use Illuminate\Support\Facades\Redis;

class GoodsController extends BaseController
{
    /**
     * 获取商品列表
     */
    public function getGoodsList(Request $request){
        $data = $request->input();
        $user = $this->users;
        $member_id = $user['user_id'];
        $list = Goods::goodsList($data,$member_id);
        if($list){
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['pact_type'] = GoodsService::getGoodsPactName($v['pact_type']);
                $list['data'][$k]['status_name'] = GoodsService::getGoodsStatusName($v['status']);
                $list['data'][$k]['ischeck_name'] = GoodsService::getGoodsCheckStatus($v['ischeck']);
                $list['data'][$k]['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
                //获取按天按月
                $rentType_arr = GoodsSetmeal::GoodsRentType($v['id']);
                $rentType ='';
                if($rentType_arr){
                    foreach (array_unique($rentType_arr) as $key=>$val){
                        $rentType .= GoodsService::GOODS_RENT_TYPE_NAME_LIST[$val].',';
                    }
                }
                if(!empty($rentType)){
                    $rentType = mb_substr($rentType,0,-1);
                }
                $list['data'][$k]['rent_type'] = $rentType;
                if(!empty($v['activity_type'])){
                    $activity_list = explode(',',$v['activity_type']);
                    foreach ($activity_list as $k1=>$v1){
                        $list['data'][$k]['activity'][$k1]['activity_title'] = GoodsService::getGoodsActivity($v1);
                        $list['data'][$k]['activity'][$k1]['activity_img'] = GoodsService::getGoodsActivityImg($v1);
                    }
                }

                $quantity= GoodsSetmeal::where([['goods_id', $v['id']],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->select('id','quantity')->get();
                $quantity = $quantity ? $quantity->toArray() : array();
                if(!empty($quantity)){
                    $stock = array_column($quantity,'quantity');
                    $list['data'][$k]['stock'] = array_sum($stock);
                    //判断库存  是否缺货
                    if(in_array(0,$stock)){
                        $list['data'][$k]['is_lack'] = 1;
                    }else{
                        $list['data'][$k]['is_lack'] = 0;
                    }
                }else{
                    $list['data'][$k]['stock'] = 0;
                    $list['data'][$k]['is_lack'] = 1;
                }
                if($v['status'] != GoodsService::GOODS_STATUS_OFF_REFUSE && $v['ischeck'] != GoodsService::GOODS_CHECK_STATUS_REFUSE){
                    $list['data'][$k]['check_reason'] = '';
                }
                if(!empty($data['goods_status'])){
                    if($data['goods_status'] == GoodsService::GOODS_CHECK_LIST){//审核中
                        if(!empty($v['checked_status'])){
                            $list['data'][$k]['status_name'] = GoodsService::getGoodsStatusName($v['checked_status']);
                        }else{
                            $list['data'][$k]['status_name'] = GoodsService::getGoodsStatusName($v['status']);
                        }
                        if($v['status'] == GoodsService::GOODS_STATUS_APPLY_OFF || $v['status'] == GoodsService::GOODS_STATUS_OFF_REFUSE){ //下架审核
                            $list['data'][$k]['status_name'] = GoodsService::GOODS_STATUS_NAME_LIST[GoodsService::GOODS_STATUS_OFF];
                            $list['data'][$k]['check_type'] = GoodsService::GOODS_OFF_CHECK_TYPE;
                            $list['data'][$k]['check_type_name'] = GoodsService::GOODS_CHECK_TYPE_LIST[GoodsService::GOODS_OFF_CHECK_TYPE];
                        }else{
                            $list['data'][$k]['check_type'] = GoodsService::GOODS_CHECK_TYPE;
                            $list['data'][$k]['check_type_name'] = GoodsService::GOODS_CHECK_TYPE_LIST[GoodsService::GOODS_CHECK_TYPE];
                        }
                        if($v['status'] == GoodsService::GOODS_STATUS_APPLY_OFF || $v['ischeck'] == GoodsService::GOODS_CHECK_STATUS_NO){ //待审核
                            $list['data'][$k]['check_status'] = GoodsService::GOODS_NO_CHECK_STATUS;
                            $list['data'][$k]['check_status_name'] = GoodsService::GOODS_CHECK_STATUS_LIST[GoodsService::GOODS_NO_CHECK_STATUS];
                        }elseif($v['status'] == GoodsService::GOODS_STATUS_OFF_REFUSE || $v['ischeck'] == GoodsService::GOODS_CHECK_STATUS_REFUSE){
                            $list['data'][$k]['check_status'] = GoodsService::GOODS_REFUSE_CHECK_STATUS;
                            $list['data'][$k]['check_status_name'] = GoodsService::GOODS_CHECK_STATUS_LIST[GoodsService::GOODS_REFUSE_CHECK_STATUS];
                        }
                    }
                }
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$list);
    }


    /**
     * 查看商品详情
     */
    public function getGoodsInfo(Request $request){
        $goods_id = $request->input('goods_id');
        if($goods_id){
            $goods = Goods::GoodsInfo($goods_id);
            if($goods){
                $goods['new_grade'] = GoodsService::getGoodsNewGrade($goods['new_grade']);
                $goods['pact_type'] = GoodsService::getGoodsPactName($goods['pact_type']);
                $goods['province'] = Areas::GetAreasById($goods['province_id']);
                $goods['city'] = Areas::GetAreasById($goods['show_ity']);
                $goods['status_name'] = GoodsService::getGoodsStatusName($goods['status']);
                $goods['convey_type1'] = GoodsService::getGoodsSendTypeName($goods['convey_type1']);
                $goods['convey_type2'] = GoodsService::getGoodsReturnTypeName($goods['convey_type2']);
                unset($goods['province_id']);
                unset($goods['show_ity']);
                unset($goods['status']);
                $goods['attribute'] = GoodsCustom::getGoodsAttribute($goods['id']);

                //套餐信息
                $setmeal = GoodsSetmeal::getGoodsSetmeal($goods['id']);
                if($setmeal){
                    foreach ($setmeal as $k=>$v){
                        if($v['lease_set'] == GoodsService::GOODS_RENT_TYPE_MONTH){//按月
                            $spec = GoodsSpec::GetSpecList($v['id'],$v['goods_id']);
                            if(!empty($spec)){
                                foreach ($spec as $key=>$val){
                                    $spec[$key]['lease_time'] = $val['lease_time'].'个月';
                                }
                            }
                        }elseif ($v['lease_set'] == GoodsService::GOODS_RENT_TYPE_DAY){//按天
                            $spec = GoodsSpec::GetSpecList($v['id'],$v['goods_id']);
                            if(!empty($spec)){
                                foreach ($spec as $key=>$val){
                                    $spec[$key]['lease_time'] = $val['lease_time'].'天';
                                }
                            }
                        }elseif ($v['lease_set'] == GoodsService::GOODS_RENT_TYPE_CUSTOM){//自定义
                            $spec = GoodsCustomRent::GetCustomRentBySetmealId($v['id']);
                            if(!empty($spec)){
                                foreach ($spec as $key=>$val){
                                    $spec[$key]['lease_days'] = $val['lease_days'].'天';
                                }
                            }
                        }
                        $setmeal[$k]['spec'] = $spec;
                        //支持花呗
                        if($v['is_huabei'] == 1){
                            $huabei_data = GoodsHuabei::GetHuabeiList($v['id']);
                            if($huabei_data){
                                foreach ($huabei_data as $k1=>$v1){
                                    $huabei_data[$k1]['three_bear'] = GoodsService::GOODS_HUABEI_FEE_LIST[$v1['three_bear']];
                                    $huabei_data[$k1]['six_bear'] = GoodsService::GOODS_HUABEI_FEE_LIST[$v1['six_bear']];
                                    $huabei_data[$k1]['twelve_bear'] = GoodsService::GOODS_HUABEI_FEE_LIST[$v1['twelve_bear']];
                                }
                            }
                            $setmeal[$k]['huabei_data'] = $huabei_data;
                        }
                        $setmeal[$k]['is_buy_out'] = GoodsService::getGoodsBuyoutName($v['is_buy_out']);
                        $setmeal[$k]['is_huabei'] = GoodsService::getGoodsIsHuabei($v['is_huabei']);
                    }
                }
                $goods['setmeal'] = $setmeal;
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$goods);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'缺少参数');
        }
    }



    /**
     * 草稿箱列表
     */
    public function getGoodsDraft(Request $request){
        $data = $request->input();
        $user = $this->users;
        $member_id = $user['user_id'];
        $list = Goods::GoodsDraft($data,$member_id);
        if($list['data']){
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]['update_time'] = date('Y-m-d H:i:s',$v['update_time']);
            }
        }
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$list);
    }


    /**
     * 草稿箱编辑 -- 获取商品信息
     */
    public function getGoodsAndSetmeal(Request $request){
        $goods_id = $request->input('goods_id');
        if($goods_id){
            $goods = Goods::GoodsInfo($goods_id);
            if($goods){
                $goods['attribute'] = GoodsCustom::getGoodsAttribute($goods['id']);
                $goods['status'] = $goods['checked_status'];
                //套餐信息
                $setmeal = GoodsSetmeal::getGoodsSetmeal($goods['id']);
                if(!empty($setmeal)){
                    foreach ($setmeal as $k=>$v){
                        $spec = GoodsSpec::GetSpecList($v['id'],$v['goods_id']);
                        if($v['lease_set'] == GoodsService::GOODS_RENT_TYPE_CUSTOM){//自定义租期
                            $custom = [];
                            foreach ($spec as $key=>$val){
                                $custom[] = GoodsCustomRent::GetCustomRent($val['id']);
                            }
                            $setmeal[$k]['spec'] = $custom;
                        }else{
                            $setmeal[$k]['spec'] = $spec;
                        }
                        if($v['is_huabei'] == GoodsService::GOODS_SETMEAL_IS_HUABEI){
                            $setmeal[$k]['huabei_data'] = GoodsHuabei::GetHuabeiList($v['id']);
                        }
                    }
                }
                $goods['setmeal'] = $setmeal;
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$goods);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL,'缺少参数');
        }
    }

    /**
     * 获取商户品类
     */
    public function getShopCategory(){
        $user = $this->users;
        $member_id = $user['user_id'];
        $category = CategoryUser::GetShopCategory($member_id);
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$category);
    }


    /**
     * 获取商品参数
     */
    public function getGoodsParameter(Request $request){
        $cate_id = $request->input('cate_id');
        if($cate_id){
            $data = GoodsCustomfield::where('category_id',$cate_id)->select('field_title','field_type','field_val as field_list','must_is')->get();
            $data = $data ? $data->toArray() : [];
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择分类');
        }
    }


    /**
     * 获取商品添加各参数列表
     */
    public function getGoodsRentData(){
        $data = [];
        $data['new_grade'] = GoodsService::GOODS_NEW_GRADE_NAME_LIST;
        $data['rent_label'] = GoodsService::GOODS_RENT_LABEL_NAME_LIST;
        $data['pact_type'] = GoodsService::GOODS_PACT_TYPE_NAME_LIST;
        $data['send_out'] = GoodsService::GOODS_SEND_CONVEY_TYPE_NAME_LIST;
        $data['return'] = GoodsService::GOODS_RETURN_CONVEY_TYPE_NAME_LIST;

        $shop_user = $this->users;
        $member_id = $shop_user['user_id'];
        $data['shop_address'] = MemberPromise::GetShopAddress($member_id);

        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
    }

    /**
     * 添加商品基础信息
     */
    public function addGoodsData(GoodsSaveRequest $request){
        $data = $request->validated();
        $shop_user = $this->users;
        $shop_data = ShopUser::where([['member_id',$shop_user['user_id']],['endisable',CommonService::SHOP_ENABLE]])->select('member_id','contract_type','sign_num','is_insurance')->first();
        $shop_data = $shop_data ? $shop_data->toArray() : [];
        if(empty($shop_data)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商户信息不存在或状态为禁用');
        }
        if(empty($data)){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '添加数据不能为空');
        }
        $res = Goods::AddGoodsData($data,$shop_data);
        if($res){
            GoodsLog::AddGoodsLog($this->users['id'],$res['data']['goods_id'],'','添加商品基础信息',json_encode($data));
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'添加成功',['goods_id'=>$res]);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
        }
    }


    /**
     * 编辑商品 -- 获取商品基础信息
     */
    public function getGoodsData(Request $request){
        $goods_id = $request->input('goods_id');
        if(!empty($goods_id)){
            $goods = Goods::GoodsInfo($goods_id);
            if(!empty($goods)){
                $goods['attribute'] = GoodsCustom::getGoodsAttribute($goods_id);
                $goods['status'] = $goods['checked_status'];
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$goods);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要编辑的商品');
        }
    }

    /**
     * 编辑商品基础信息
     */
    public function editGoodsData(GoodsSaveRequest $request){
        $data = $request->validated();
        if(empty($data['goods_id'])){
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要编辑的商品');
        }
        $shop_user = $this->users;
        $res = Goods::EditGoodsData($data);
        if($res['status'] == CommonService::HTTP_CODE_SUCCESS){
            GoodsLog::AddGoodsLog($this->users['id'],$data['goods_id'],'','编辑商品基础信息:'.$res['data']['operation_explain'],json_encode($data));
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'编辑成功',$res['data']);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
        }
    }


    /**
     * 商品分享
     */
    public function goodsShare(Request $request)
    {
        $goods_id = $request->input('goods_id');
        if ($goods_id) {
            $goods = Goods::where([['id', $goods_id], ['status', GoodsService::GOODS_STATUS_PUT], ['is_delete', GoodsService::GOODS_NO_DELETE_STATUS]])->select('id', 'title', 'imgurl', 'min_price')->first();
            $goods = $goods ? $goods->toArray() : array();
            if (!empty($goods)) {
                $imageInfo = getimagesize(config('alipay.oss.imgHost') . $goods['imgurl']);
                $goods['imgurl'] = 'data:' . $imageInfo['mime'] . ';base64,' . chunk_split(base64_encode(file_get_contents(config('alipay.oss.imgHost') . $goods['imgurl'])));
                $goods['alipay_url'] = config('alipay.xxz.alipayUrl') . 'pages/productDetails/productDetails&query=goodsId=' . $goods_id . '&from=qrcode';
                $res = AlipayService::createQrCode(10001, $goods_id, $goods['title']);
                if ($res['status']) {
                    $goods['qrcode_url'] = $res['data']['qr_code_url'];
                }
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS, '获取成功', $goods);
            } else {
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '上架中的商品才能分享');
            }
        } else {
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择商品');
        }
    }


    /**
     * 商品下架、申请下架
     */
    public function itemDownShelf(Request $request){
        $goods_id = $request->input('goods_id');
        $shop_user = $this->users;
        $member_id = $shop_user['user_id'];
        if($goods_id){
            $res = Goods::ItemDownShelf($goods_id,$member_id);
            if($res['status'] == CommonService::HTTP_CODE_SUCCESS){
                if($res['data']['operation_type'] == 2){//商品下架
                    Redis::hdel(GoodsService::GOODS_INFO_KEY,$goods_id);
                }
                GoodsLog::AddGoodsLog($this->users['id'],$goods_id,'',$res['data']['operation_explain'],'');
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要下架的商品');
        }
    }


    /**
     * 商品上架
     */
    public function goodsGrounding(Request $request){
        $goods_id = $request->input('goods_id');
        $shop_user = $this->users;
        $member_id = $shop_user['user_id'];
        if($goods_id){
            $res = Goods::GoodsGrounding($goods_id,$member_id);
            if($res['status'] == CommonService::HTTP_CODE_SUCCESS){
                //上架成功添加缓存
                $goods = Goods::getGoodsInfo($goods_id);
                if(!empty($goods)){
                    $goods['month_rent'] = ceil($goods['min_price'] * 30);
                    $goods['max_foregift'] = GoodsSetmeal::getMaxForegift($goods_id);
                    $shop_category = CategoryUser::GetShopCategory($goods['member_id']);
                    $goods['category'] = implode('、',array_column($shop_category,'name'));
                    $goods['convey_type'] = '寄出'.GoodsService::GOODS_SEND_CONVEY_TYPE_NAME_LIST[$goods['convey_type1']].'/归还'.GoodsService::GOODS_SEND_CONVEY_TYPE_NAME_LIST[$goods['convey_type2']];
                    unset($goods['convey_type1']);
                    unset($goods['convey_type2']);
                    Redis::hset(GoodsService::GOODS_INFO_KEY,$goods_id,json_encode($goods));
                }
                GoodsLog::AddGoodsLog($this->users['id'],$goods_id,'','商品上架','');
                //租赁宝上传商品
//                ZlbService::toTask([$goods_id],ZlbService::TYPE_GOODS);
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'上架成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要上架的商品');
        }
    }

    /**
     * 提交审核
     */
    public function goodsSubmitReview(Request $request){
        $goods_id = $request->input('goods_id');
        $shop_user = $this->users;
        $member_id = $shop_user['user_id'];
        if($goods_id){
            $res = Goods::SubmitReview($goods_id,$member_id);
            if($res['status'] == CommonService::HTTP_CODE_SUCCESS){
                GoodsLog::AddGoodsLog($this->users['id'],$goods_id,'','商品提交审核','');
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要上架的商品');
        }
    }

    /**
     * 取消审核
     */
    public function goodsCancelReview(Request $request){
        $goods_id = $request->input('goods_id');
        $shop_user = $this->users;
        $member_id = $shop_user['user_id'];
        if($goods_id){
            $res = Goods::CancelReview($goods_id,$member_id);
            if($res['status'] == CommonService::HTTP_CODE_SUCCESS){
                GoodsLog::AddGoodsLog($this->users['id'],$goods_id,'','商品取消审核','');
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的商品');
        }
    }


    /**
     * 删除商品
     */
    public function deleteGoods(Request $request){
        $goods_id = $request->input('goods_id');
        $shop_user = $this->users;
        $member_id = $shop_user['user_id'];
        if($goods_id){
            $res = Goods::GoodsDelete($goods_id,$member_id);
            if($res['status'] == CommonService::HTTP_CODE_SUCCESS){
                GoodsLog::AddGoodsLog($this->users['id'],$goods_id,'','删除商品','');
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'删除成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要删除的商品');
        }
    }

    /**
     * 删除草稿单条
     */
    public function deleteGoodsDraft(Request $request){
        $goods_id = $request->input('goods_id');
        $shop_user = $this->users;
        $member_id = $shop_user['user_id'];
        if($goods_id){
            $res = Goods::GoodsDelete($goods_id,$member_id);
            if($res['status'] == CommonService::HTTP_CODE_SUCCESS){
                GoodsLog::AddGoodsLog($this->users['id'],$goods_id,'','删除商品','');
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'删除成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要删除的商品');
        }
    }

    /**
     * 批量删除草稿
     */
    public function batchDeleteGoodsDraft(Request $request){
        $goods_id = $request->input('goods_id');
        $shop_user = $this->users;
        $member_id = $shop_user['user_id'];
        if(!empty($goods_id)){
            $goodsid = explode(',',$goods_id);
            $res = Goods::BatchDeleteDraft($goodsid,$member_id);
            if($res['status'] == CommonService::HTTP_CODE_SUCCESS){
                GoodsLog::AddGoodsLog($this->users['id'],'','','批量删除草稿',json_encode($goods_id));
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'删除成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要删除的商品');
        }
    }



    /**
     * 获取起租日列表和租期间隔
     */
    public function getParameterList(){
        $data = [];
        $data['start_date'] = GoodsService::GOODS_START_DATE_LIST;
        $data['withhold_cycle'] = GoodsService::GOODS_WITHHOLD_CYCLE_LIST;
        $data['lease_term_month'] = GoodsService::GOODS_LEASE_TERM_MONTH_LIST;
        return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
    }


    /**
     * 添加套餐信息
     */
    public function addSetmeal(Request $request){
        $data = $request->input();
        $shop_user = $this->users;
        $res = Goods::DealGoodsMealSpecData($data);
        if($res == 200){
            if($data['operation_type'] == GoodsService::GOODS_PUBLISH){ //发布商品
                Goods::where('id',$data['goods_id'])->update(['ischeck'=>GoodsService::GOODS_CHECK_STATUS_NO,'checked_status'=>$data['status'],'uid'=>$shop_user['id'],'update_time'=>time()]);
            }else{
                Goods::where('id',$data['goods_id'])->update(['checked_status'=>$data['status'],'uid'=>$shop_user['id'],'update_time'=>time()]);
            }
            GoodsLog::AddGoodsLog($this->users['id'],$data['goods_id'],$res['data']['setmeal_id'],'添加套餐信息',json_encode($data));
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'添加成功',$res['data']);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
        }
    }

    /**
     * 编辑套餐 --- 获取商品套餐列表
     */
    public function getGoodsSetmealList(Request $request){
        $goods_id = $request->input('goods_id');
        if(!empty($goods_id)){
            $goods = Goods::where('id',$goods_id)->select('id','title')->first();
            $goods = $goods ? $goods->toArray() : array();
            if(!empty($goods)){
                $data = Goods::rentGoodsMealSpecInfo($goods_id);
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$data);
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商品信息不存');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要编辑的商品');
        }
    }

    /**
     * 编辑套餐---获取套餐信息
     */
    public function getGoodsSetmealInfo(Request $request){
        $setmeal_id = $request->input('setmeal_id');
        if(!empty($setmeal_id)){
            $goods_setmeal = GoodsSetmeal::getGoodsSetmealInfo($setmeal_id);
            if($goods_setmeal){
                $goods = Goods::where('id',$goods_setmeal['goods_id'])->select('id','pact_type','ischeck','checked_status')->first();
                $goods = $goods ? $goods->toArray() : array();
                if(!empty($goods)){
                    $goods_setmeal['pact_type'] = $goods['pact_type'];
                    $goods_setmeal['ischeck'] = $goods['ischeck'];
                    $goods_setmeal['goods_status'] = $goods['checked_status'];
                }
                $spec = GoodsSpec::GetSpecList($setmeal_id,$goods['id']);
                if($goods_setmeal['lease_set'] == GoodsService::GOODS_RENT_TYPE_CUSTOM){//自定义租期
                    $custom = [];
                    foreach ($spec as $k=>$v){
                        $custom[] = GoodsCustomRent::GetCustomRent($v['id']);
                    }
                    $goods_setmeal['spec'] = $custom;
                }else{
                    $goods_setmeal['spec'] = $spec;
                }
                if($goods_setmeal['is_huabei'] == GoodsService::GOODS_SETMEAL_IS_HUABEI){
                    $goods_setmeal['huabei_data'] = GoodsHuabei::GetHuabeiList($setmeal_id);
                }
            }
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'获取成功',$goods_setmeal);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要编辑的套餐');
        }
    }

    /**
     * 编辑商品套餐信息
     */
    public function editGoodsSetmeal(Request $request){
        $data = $request->input();
        $shop_user = $this->users;
        $res = Goods::DealGoodsMealSpecData($data);
        if($res){
            if($data['operation_type'] == GoodsService::GOODS_PUBLISH){ //发布商品
                Goods::where('id',$data['goods_id'])->update(['ischeck'=>GoodsService::GOODS_CHECK_STATUS_NO,'checked_status'=>$data['status'],'uid'=>$shop_user['id'],'update_time'=>time()]);
            }else{
                Goods::where('id',$data['goods_id'])->update(['checked_status'=>$data['status'],'uid'=>$shop_user['id'],'update_time'=>time()]);
            }
            GoodsLog::AddGoodsLog($this->users['id'],$data['goods_id'],$data['setmeal_id'],'编辑商品套餐信息',json_encode($data));
            return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'编辑成功',$res['data']);
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
        }
    }

    /**
     * 获取套餐库存
     */
    public function getSetmealStock(Request $request){
        $goods_id = $request->input('goods_id');
        if($goods_id){
            $shop_user = $this->users;
            $member_id = $shop_user['user_id'];
            if(Goods::where([['id',$goods_id],['member_id',$member_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->count() > 0){
                $setmeal = GoodsSetmeal::where([['goods_id',$goods_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
                    ->select('id','title','quantity')->get();
                $setmeal = $setmeal ? $setmeal->toArray() : [];
                if(!empty($setmeal)){
                    foreach ($setmeal as $k=>$v){
                        $lease = GoodsSpec::where([['goods_id',$goods_id],['setmeal_id',$v['id']],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])
                            ->select('lease_price')->get();
                        $lease = $lease ? $lease->toArray() : [];
                        if(!empty($lease)){
                            $setmeal[$k]['price'] = min(array_column($lease,'lease_price')).'-'.max(array_column($lease,'lease_price'));
                        }else{
                            $setmeal[$k]['price'] = '-';
                        }
                    }
                }
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'编辑成功',$setmeal);
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商品不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的商品');
        }
    }

    /**
     * 修改库存
     */
    public function editGoodsStock(Request $request){
        $goods_id = $request->input('goods_id');
        $setmeal = $request->input('setmeal');
        if($goods_id && $setmeal){
            $shop_user = $this->users;
            $member_id = $shop_user['user_id'];
            if(Goods::where([['id',$goods_id],['member_id',$member_id],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->count() > 0){
                foreach ($setmeal as $k=>$v){
                    if(!empty($v['id']) && $v['stock'] >= 0){
                        GoodsSetmeal::where([['goods_id',$goods_id],['id',$v['id']],['is_delete',GoodsService::GOODS_NO_DELETE_STATUS]])->update(['quantity'=>$v['stock']]);
                        GoodsLog::AddGoodsLog($this->users['id'],$goods_id,$v['id'],'修改商品库存，修改后库存'.$v['stock'],$v['stock']);
                    }
                }
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'操作成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, '商品不存在');
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要操作的商品');
        }
    }


    /**
     * 删除套餐
     */
    public function deleteGoodsSetmeal(Request $request){
        $goods_id = $request->input('goods_id');
        $setmeal_id = $request->input('setmeal_id');
        $shop_user = $this->users;
        $member_id = $shop_user['user_id'];
        if($goods_id){
            $res = GoodsSetmeal::DeleteStemeal($goods_id,$member_id,$setmeal_id);
            if($res['status'] == CommonService::HTTP_CODE_SUCCESS){
                GoodsLog::AddGoodsLog($this->users['id'],$goods_id,$setmeal_id,'删除套餐','');
                return ReturnMsg(CommonService::HTTP_CODE_SUCCESS,'删除成功');
            }else{
                return ReturnMsg(CommonService::HTTP_CODE_FAIL, $res['msg']);
            }
        }else{
            return ReturnMsg(CommonService::HTTP_CODE_FAIL, '请选择要删除的商品');
        }
    }



}
